package com.laomuji.android.mvvm.utils.permission

import android.app.Activity
import androidx.core.app.ActivityCompat
import com.laomuji.android.mvvm.utils.PackageUtil

/**
 * 权限申请
 * 具体的权限申请操作会在这里进行
 * @author laomuji666
 * @since 2024/2/20
 */
class PermissionRequest private constructor(private val permissionCallback: PermissionCallback?) {
    object Key {
        //权限回调标识
        const val REQUEST_CODE = 0
    }

    //需要的权限
    private val requiredPermissions: ArrayList<String> = ArrayList()

    //需要申请的权限
    private val requestPermissions: ArrayList<String> = ArrayList()

    //已经授权的权限
    private val grantedPermissions: ArrayList<String> = ArrayList()

    //临时拒绝的权限
    private val deniedPermissions: ArrayList<String> = ArrayList()

    //永久拒绝的权限
    private val foreverDeniedPermissions: ArrayList<String> = ArrayList()

    constructor(permissionCallback: PermissionCallback?, vararg permissions: String) : this(
        permissionCallback
    ) {
        for (permission in permissions) {
            requiredPermissions.add(permission)
        }
    }

    fun requestPermission() {
        //检查所有需要的权限,分为已申请和需要申请
        for (permission in requiredPermissions) {
            if (PermissionUtil.isGrantedPermission(permission)) {
                grantedPermissions.add(permission)
            } else {
                requestPermissions.add(permission)
            }
        }

        //如果所有权限都已经拥有,直接回调
        if (grantedPermissions.size == requiredPermissions.size) {
            handlePermissionCallback()
            return
        }


        //有需要申请的权限,申请权限
        ActivityCompat.requestPermissions(
            PackageUtil.getTopActivity(),
            requestPermissions.toTypedArray(),
            Key.REQUEST_CODE
        )
    }

    /**
     * 授权回调
     * 在[Activity.onRequestPermissionsResult]方法中调用此方法
     */
    fun onRequestPermissionsResult() {
        //授权回调时,检查申请的权限
        for (permission in requestPermissions) {
            if (PermissionUtil.isGrantedPermission(permission)) {
                //授权的权限
                grantedPermissions.add(permission)
            } else {
                //拒绝的权限,检查拒绝状态
                if (ActivityCompat.shouldShowRequestPermissionRationale(
                        PackageUtil.getTopActivity(),
                        permission
                    )
                ) {
                    //临时拒绝的权限
                    deniedPermissions.add(permission)
                } else {
                    //永久拒绝的权限
                    foreverDeniedPermissions.add(permission)
                }
            }
        }
        handlePermissionCallback()
    }


    /**
     * 处理权限回调
     * 优先级从上往下依次触发
     * 1.存在永久拒绝的权限,至少有1条触发[PermissionCallback.onForeverDenied]
     * 2.存在临时拒绝的权限,至少有1条触发[PermissionCallback.onDenied]
     * 3.授权了所有权限,触发[PermissionCallback.onGranted]
     */
    private fun handlePermissionCallback() {
        //没有设置回调,直接返回
        if (permissionCallback == null) {
            return
        }

        //存在永久拒绝的权限
        if (foreverDeniedPermissions.size > 0) {
            permissionCallback.onForeverDenied(foreverDeniedPermissions)
            return
        }

        //存在临时拒绝的权限
        if (deniedPermissions.size > 0) {
            permissionCallback.onDenied(deniedPermissions)
            return
        }

        //授权了所有需要的权限
        permissionCallback.onGranted()
    }
}